program Lawnmower;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

const
  no = 'NO';
  yes = 'YES';
var
  f, g: TextFile;
  n, m: Byte;
  a: array [0..99] of array [0..99] of Integer;
  verdict: String;
  verdictAssigned: Boolean;

procedure ReadData;
var
  i, j: Byte;
begin
  Readln(f, n, m);
  for i := 0 to n - 1 do begin
    for j := 0 to m - 1 do begin
      Read(f, a[i, j]);
      if (a[i, j] < 1) or (a[i, j] > 100) then begin
        verdictAssigned := true;
        verdict := no;
      end;
    end;
    Readln(f);
  end;
  if not verdictAssigned then
    if (n = 1) or (m = 1) then begin
      verdictAssigned := true;
      verdict := yes;
    end;
end;

function RowHasBiggerElement(row, el: Byte): Boolean;
var
  i: Byte;
begin
  for i := 0 to m - 1 do begin
    if (a[Row, i] > el) then begin
      Result := true;
      Exit;
    end;
  end;
  Result := false;
end;

function ColHasBiggerElement(col, el: Byte): Boolean;
var
  i: Byte;
begin
  for i := 0 to n - 1 do begin
    if (a[i, Col] > el) then begin
      Result := true;
      Exit;
    end;
  end;
  Result := false;
end;

procedure Check(Row, Col: Byte);
var
  el: Byte;
  rh, ch: Boolean;
begin
  el := a[Row, Col];
  rh := RowHasBiggerElement(Row, el);
  ch := ColHasBiggerElement(Col, el);
  if (rh and ch) then begin
    verdict := no;
    verdictAssigned := true;
  end;
end;

procedure ProcessData;
var
  i, j: Byte;
begin
  for i := 0 to n - 1 do
    for j := 0 to m - 1 do begin
      Check(i, j);
      if verdictAssigned then
        Exit;
    end;
  verdict := yes;
end;

procedure OutputVerdict(casenum: Word);
begin
  writeln(g, 'Case #', casenum, ': ', verdict);
end;

var
  i, t: Byte;

begin
  try
    AssignFile(f, 'input.txt');
    Reset(f);
    AssignFile(g, 'output.txt');
    Rewrite(g);
    try
      Readln(f, t);
      for i := 1 to t do begin
        verdict := '';
        verdictAssigned := false;
        ReadData;
        if not verdictAssigned then
          ProcessData;
        OutputVerdict(i);
      end;
    finally
      CloseFile(f);
      CloseFile(g);
    end;
    writeln('Data is processed, program is going to terminate');
    readln;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;
end.
